﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {

        // Update Window
        private Thread MyUpdateWindow;
        // Feeder, Start , Stop        
        private Thread myFeedThread;

        // Data / Data Update
        private static PricesData myPricesData;

        public Form1()
        {
            InitializeComponent();

            myPricesData = new PricesData();
            myPricesData.setTheDataGridView(this.dataGrid_Prices);
            Feed.set_PricesData(myPricesData);

            myFeedThread = new Thread(new ThreadStart(Feed.Feeding));
            myFeedThread.Name = "Feeder";
            myFeedThread.IsBackground = true;
            myFeedThread.Start();

            UpdateWinForm.setTheDataGridView(myPricesData);
            MyUpdateWindow = new Thread(new ThreadStart(UpdateWinForm.UpdateLoop));
            MyUpdateWindow.Name = "WinformUpdate";
            MyUpdateWindow.IsBackground = true;
            MyUpdateWindow.Start();
        }

        ~Form1()
        {
            MyUpdateWindow.Abort();
            myFeedThread.Abort();
        }

        private void start_button_Click(object sender, EventArgs e)
        {
            int waitingTime;
            if (int.TryParse(waitingTime_textBox.Text, out waitingTime))
            {
                Feed.timeWait = waitingTime;
                Feed.myEventWaitHandle.Set();     
            }
        }

        private void stop_button_Click(object sender, EventArgs e)
        {
            Feed.myEventWaitHandle.Reset(); 
        }

        private void Add_Row_button_Click(object sender, EventArgs e)
        {
            int theID;
            double theValue;
            if (int.TryParse(ID_textBox.Text,out theID) && double.TryParse(Value_textBox.Text,out theValue) )
            {
                myPricesData.add_Row(theID, theValue);
                Console.WriteLine("theID" + theID.ToString() + "  theValue : " + theValue.ToString());
                ID_textBox.Text = (theID + 1).ToString() ;
            }
            
        }

    }
}
